<?php

require_once("Love.class.php");
$love = new Love();

function code2utf($num){
	//Returns the utf string corresponding to the unicode value
	if($num<128)return chr($num);
	if($num<2048)return chr(($num>>6)+192).chr(($num&63)+128);
	if($num<65536)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
	if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128). chr(($num&63)+128);
	return '';
}
function utf2utf($str) {
	$patterns = array('/Đ/', '/Č/', '/Ć/', '/Ž/', '/š/', '/đ/', '/č/', '/ć/', '/ž/', '/š/', '/'.code2utf(352).'/');
	$replaces = array('&#272;', '&#268;', '&#262;', '&#381;', '&#353;', '&#273;', '&#269;', '&#263;', '&#382;', '&#353;', '&#352;');
	return preg_replace($patterns, $replaces, $str);
}

if ($_POST) {
// 	print_r($_POST);
// 	
// 	echo utf2utf($_POST['ime_prezime']);
// 	
// 	die();
	if ($dodaj) {
		if (!$ime_prezime || !$pozicija || !$idbr || !$region || !$datum) {
			die ("Greska: Morate popuniti sva polja!");
		}
		require_once ('Image_Toolbox.class.php');
		$img = new Image_Toolbox('images/idcard.jpg');
		$ime_prezime = utf2utf($ime_prezime);
		
		// Da li je ime i prezime vise od 14 karaktera
		if (strlen($ime_prezime) > 24) {
			$size = 40;
// 			if (strlen($ime_prezime) > 17) {
// 				$size = 40;
// 				if (strlen($ime_prezime) > 22) {
// 					$size = 35;
// 				}
// 			}
		}else{
			$size = 50;
		}

// 		$img->addText($ime_prezime, "fonts\arialnb.ttf", $size, "#000000", '600', '356', "0");
// 		$img->addText($pozicija, "fonts\arialbd.ttf", 30, "#000000", '600', '434', "0");
// 		$img->addText($idbr, "fonts\arialbd.ttf", 30, "#000000", '700', '504', "0");
// 		$img->addText($region, "fonts\arialbd.ttf", 30, "#000000", '720', '574', "0");
// 		$img->addText($datum, "fonts\arialbd.ttf", 30, "#000000", '730', '631', "0");

		$img->addText($ime_prezime, "fonts\arialnb.ttf", $size, "#000000", '457', '386', "0");
		$img->addText($pozicija, "fonts\arialbd.ttf", 30, "#000000", '462', '446', "0");
		$img->addText($idbr, "fonts\arialbd.ttf", 30, "#000000", '527', '499', "0");
		$img->addText($region, "fonts\arialbd.ttf", 30, "#000000", '537', '548', "0");
		$img->addText($datum, "fonts\arialbd.ttf", 30, "#000000", '545', '595', "0");


// 		$img->addText($ime_prezime, "fonts\arialnb.ttf", $size, "#000000", '600', '515', "0");
// 		$img->addText($pozicija, "fonts\arialbd.ttf", 30, "#000000", '600', '597', "0");
// 		$img->addText($idbr, "fonts\arialbd.ttf", 30, "#000000", '700', '665', "0");
// 		$img->addText($region, "fonts\arialbd.ttf", 30, "#000000", '720', '730', "0");
// 		$img->addText($datum, "fonts\arialbd.ttf", 30, "#000000", '730', '795', "0");
 		$img->save("images/create/".time().".jpg");
	}
	if ($novo) {
		if ($dh = opendir("images/create")) { 
	    	while (($file = readdir($dh)) !== false) { 
	    		if ($file != "." && $file != "..") {
	    			unlink("images/create/".$file);
	    		}
	    	} 
	    	closedir($dh);
	   	} 
	}
	if ($stampaj1) {
		header("Location: index.php?strana=prednja&print=1");
	}
	if ($stampaj2) {
		header("Location: index.php?strana=zadnja&print=1");
	}
}



if ($show == "back") {
	$love->assign("strana", "zadnja");
}else{
	$love->assign("strana", "prednja");
}

if ($strana == "prednja") {
	if ($print) {
		$templ = "<html><head></head><body bgcolor=\"#ffffff\" onload=\"window.print();\"><div style=\"margin-left: 30px;\">";
	}else{
		$templ = "<html><head></head><body bgcolor=\"#ffffff\"><div style=\"margin-left: 30px;\">";
	}
	$ftempl = "</div></body></html>";
	if ($dh = opendir("images/create")) { 
    	while (($file = readdir($dh)) !== false) { 
    		if ($file != "." && $file != "..") {
    			$slike[] = $file;
    		}
    	} 
    	closedir($dh);
    	//print_r($slike);
   	} 
	$x=1;
	if ($broj = count($slike)) {
		//$templ .= "<div>";
	   	foreach ($slike as $slika) {
	   		$templ .= "<img src=\"images/create/".$slika."\" width=\"337\" height=\"228\" style=\"float: left;\" />\n";
	   		if(($x - (2 * floor($x/2))) == 0) {
	   			$templ .= "<br />";
	   		}else{
	   			if ($x == $broj) {
	   				$templ .= "<div style=\"width: 337; height: 228; float: left;\"></div>";
	   			}
	   		}
	   		$x++;
	   	}
   	}else{
   		$templ .= "Dokument je prazan.";
   	}
   	$templ .= $ftempl;
   	echo $templ;
}elseif($strana == "zadnja") {
	$templ = "<html><head></head><body bgcolor=\"#ffffff\"><div style=\"margin-left: 30px;\">";
	$ftempl = "</div></body></html>";
	for ($x=1; $x<=8; $x++) {
		$templ .= "<img src=\"images/idcard_nazad.jpg\" width=\"337\" height=\"228\" />\n";
		if(($x - (2 * floor($x/2))) == 0) {
	   		$templ .= "<br />";
	   	}
	}
	$templ .= $ftempl;
   	echo $templ;
}else{
	$love->display("header.html");
	$love->display("forma_kartica.html");
	$love->display("footer.html");
}

function strcod ($in) {
	# Variable for output data to be writen.
	$out = "";
	for ($i=0; $i < strlen($in); $i++) {
	# $char pass ASCII code of current letter.
	$char = ord($in[$i]);
	# Ternary statement is used in the following line, it is checked if the code is greater than 192 (i.e. Cyrillic letter) then the symbol is converted in it's Unicode analog and is appended in the end of the string, if it isn't greater (i.e. non-Cyrillic letter) then it is simply appended in the end of the string without any conversion.
	$out .= ($char > 192)?"&#".(1040+($char-192)).";":$in[$i];
	# Return the converted string.
	}
	return $out;
}

?>